/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package it.apuliait.model;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ProxyUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;

import it.apuliait.service.ClpSerializer;
import it.apuliait.service.DocumentoXmlLocalServiceUtil;

import java.io.Serializable;

import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Donatello
 */
public class DocumentoXmlClp extends BaseModelImpl<DocumentoXml>
	implements DocumentoXml {
	public DocumentoXmlClp() {
	}

	@Override
	public Class<?> getModelClass() {
		return DocumentoXml.class;
	}

	@Override
	public String getModelClassName() {
		return DocumentoXml.class.getName();
	}

	@Override
	public long getPrimaryKey() {
		return _documentoXmlId;
	}

	@Override
	public void setPrimaryKey(long primaryKey) {
		setDocumentoXmlId(primaryKey);
	}

	@Override
	public Serializable getPrimaryKeyObj() {
		return _documentoXmlId;
	}

	@Override
	public void setPrimaryKeyObj(Serializable primaryKeyObj) {
		setPrimaryKey(((Long)primaryKeyObj).longValue());
	}

	@Override
	public Map<String, Object> getModelAttributes() {
		Map<String, Object> attributes = new HashMap<String, Object>();

		attributes.put("documentoXmlId", getDocumentoXmlId());
		attributes.put("documentoIDFK", getDocumentoIDFK());
		attributes.put("annoCreazione", getAnnoCreazione());
		attributes.put("documentoXml", getDocumentoXml());

		return attributes;
	}

	@Override
	public void setModelAttributes(Map<String, Object> attributes) {
		Long documentoXmlId = (Long)attributes.get("documentoXmlId");

		if (documentoXmlId != null) {
			setDocumentoXmlId(documentoXmlId);
		}

		Long documentoIDFK = (Long)attributes.get("documentoIDFK");

		if (documentoIDFK != null) {
			setDocumentoIDFK(documentoIDFK);
		}

		String annoCreazione = (String)attributes.get("annoCreazione");

		if (annoCreazione != null) {
			setAnnoCreazione(annoCreazione);
		}

		String documentoXml = (String)attributes.get("documentoXml");

		if (documentoXml != null) {
			setDocumentoXml(documentoXml);
		}
	}

	@Override
	public long getDocumentoXmlId() {
		return _documentoXmlId;
	}

	@Override
	public void setDocumentoXmlId(long documentoXmlId) {
		_documentoXmlId = documentoXmlId;

		if (_documentoXmlRemoteModel != null) {
			try {
				Class<?> clazz = _documentoXmlRemoteModel.getClass();

				Method method = clazz.getMethod("setDocumentoXmlId", long.class);

				method.invoke(_documentoXmlRemoteModel, documentoXmlId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public long getDocumentoIDFK() {
		return _documentoIDFK;
	}

	@Override
	public void setDocumentoIDFK(long documentoIDFK) {
		_documentoIDFK = documentoIDFK;

		if (_documentoXmlRemoteModel != null) {
			try {
				Class<?> clazz = _documentoXmlRemoteModel.getClass();

				Method method = clazz.getMethod("setDocumentoIDFK", long.class);

				method.invoke(_documentoXmlRemoteModel, documentoIDFK);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getAnnoCreazione() {
		return _annoCreazione;
	}

	@Override
	public void setAnnoCreazione(String annoCreazione) {
		_annoCreazione = annoCreazione;

		if (_documentoXmlRemoteModel != null) {
			try {
				Class<?> clazz = _documentoXmlRemoteModel.getClass();

				Method method = clazz.getMethod("setAnnoCreazione", String.class);

				method.invoke(_documentoXmlRemoteModel, annoCreazione);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getDocumentoXml() {
		return _documentoXml;
	}

	@Override
	public void setDocumentoXml(String documentoXml) {
		_documentoXml = documentoXml;

		if (_documentoXmlRemoteModel != null) {
			try {
				Class<?> clazz = _documentoXmlRemoteModel.getClass();

				Method method = clazz.getMethod("setDocumentoXml", String.class);

				method.invoke(_documentoXmlRemoteModel, documentoXml);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	public BaseModel<?> getDocumentoXmlRemoteModel() {
		return _documentoXmlRemoteModel;
	}

	public void setDocumentoXmlRemoteModel(BaseModel<?> documentoXmlRemoteModel) {
		_documentoXmlRemoteModel = documentoXmlRemoteModel;
	}

	public Object invokeOnRemoteModel(String methodName,
		Class<?>[] parameterTypes, Object[] parameterValues)
		throws Exception {
		Object[] remoteParameterValues = new Object[parameterValues.length];

		for (int i = 0; i < parameterValues.length; i++) {
			if (parameterValues[i] != null) {
				remoteParameterValues[i] = ClpSerializer.translateInput(parameterValues[i]);
			}
		}

		Class<?> remoteModelClass = _documentoXmlRemoteModel.getClass();

		ClassLoader remoteModelClassLoader = remoteModelClass.getClassLoader();

		Class<?>[] remoteParameterTypes = new Class[parameterTypes.length];

		for (int i = 0; i < parameterTypes.length; i++) {
			if (parameterTypes[i].isPrimitive()) {
				remoteParameterTypes[i] = parameterTypes[i];
			}
			else {
				String parameterTypeName = parameterTypes[i].getName();

				remoteParameterTypes[i] = remoteModelClassLoader.loadClass(parameterTypeName);
			}
		}

		Method method = remoteModelClass.getMethod(methodName,
				remoteParameterTypes);

		Object returnValue = method.invoke(_documentoXmlRemoteModel,
				remoteParameterValues);

		if (returnValue != null) {
			returnValue = ClpSerializer.translateOutput(returnValue);
		}

		return returnValue;
	}

	@Override
	public void persist() throws SystemException {
		if (this.isNew()) {
			DocumentoXmlLocalServiceUtil.addDocumentoXml(this);
		}
		else {
			DocumentoXmlLocalServiceUtil.updateDocumentoXml(this);
		}
	}

	@Override
	public DocumentoXml toEscapedModel() {
		return (DocumentoXml)ProxyUtil.newProxyInstance(DocumentoXml.class.getClassLoader(),
			new Class[] { DocumentoXml.class }, new AutoEscapeBeanHandler(this));
	}

	@Override
	public Object clone() {
		DocumentoXmlClp clone = new DocumentoXmlClp();

		clone.setDocumentoXmlId(getDocumentoXmlId());
		clone.setDocumentoIDFK(getDocumentoIDFK());
		clone.setAnnoCreazione(getAnnoCreazione());
		clone.setDocumentoXml(getDocumentoXml());

		return clone;
	}

	@Override
	public int compareTo(DocumentoXml documentoXml) {
		long primaryKey = documentoXml.getPrimaryKey();

		if (getPrimaryKey() < primaryKey) {
			return -1;
		}
		else if (getPrimaryKey() > primaryKey) {
			return 1;
		}
		else {
			return 0;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (!(obj instanceof DocumentoXmlClp)) {
			return false;
		}

		DocumentoXmlClp documentoXml = (DocumentoXmlClp)obj;

		long primaryKey = documentoXml.getPrimaryKey();

		if (getPrimaryKey() == primaryKey) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return (int)getPrimaryKey();
	}

	@Override
	public String toString() {
		StringBundler sb = new StringBundler(9);

		sb.append("{documentoXmlId=");
		sb.append(getDocumentoXmlId());
		sb.append(", documentoIDFK=");
		sb.append(getDocumentoIDFK());
		sb.append(", annoCreazione=");
		sb.append(getAnnoCreazione());
		sb.append(", documentoXml=");
		sb.append(getDocumentoXml());
		sb.append("}");

		return sb.toString();
	}

	@Override
	public String toXmlString() {
		StringBundler sb = new StringBundler(16);

		sb.append("<model><model-name>");
		sb.append("it.apuliait.model.DocumentoXml");
		sb.append("</model-name>");

		sb.append(
			"<column><column-name>documentoXmlId</column-name><column-value><![CDATA[");
		sb.append(getDocumentoXmlId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>documentoIDFK</column-name><column-value><![CDATA[");
		sb.append(getDocumentoIDFK());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>annoCreazione</column-name><column-value><![CDATA[");
		sb.append(getAnnoCreazione());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>documentoXml</column-name><column-value><![CDATA[");
		sb.append(getDocumentoXml());
		sb.append("]]></column-value></column>");

		sb.append("</model>");

		return sb.toString();
	}

	private long _documentoXmlId;
	private long _documentoIDFK;
	private String _annoCreazione;
	private String _documentoXml;
	private BaseModel<?> _documentoXmlRemoteModel;
}